package assignment.ton.jenerator;
import assignment.ton.def.Class;
import assignment.ton.def.Method;
import assignment.ton.def.Parameter;
import java.util.ArrayList;
/**
 * Creates java methods for export from a class object
 * @author Tim
 *
 */
/*
 String[] extraCode = new String[theClass.getExtraCode().size()];
		for(int i=0; i<extraCode.length;i++){
			extraCode[i] = theClass.getExtraCode().get(i);
		}
 */
public class MethJen {
	private Class theClass;
	public MethJen(Class c){
		theClass = c;
	}
	private int extraCodeLength(Method m){
		if(m.getExtraCode()!=null){
			return m.getExtraCode().size();
		}else{
			return 0;
		}
	}
	public String[] addTabs(String[] t){
		String[] out = new String[t.length];
		for(int i=0; i<t.length;i++){
			out[i] = "\t\t"+t[i];
		}
		return out;
	}
	public String[] getOutput(){
		ArrayList<Method> methods = theClass.getMethodArrayList();
		String[] data;
		int k = 0;
		for(Method m: methods){
			k=k+extraCodeLength(m);
		}
		if(methods.size()>0){
			data = new String[methods.size()*2+k];
			//for(int i = 0; i<methods.size(); i=i+3){
			int i = 0;
			for(Method m: methods){
				ArrayList<Parameter> parArray = m.getParameterArrayList();
				String constructorItems = "";
				if(parArray.size()>0){
					for(int j = 0; j<parArray.size(); j++){
						String lastComma = "";
						if((j+1)<parArray.size()){
							lastComma = ", ";
						}
						if(parArray.get(j).isMany()){
							constructorItems = constructorItems+"ArrayList<"+parArray.get(j).getType()+"> "+parArray.get(j).getName()+lastComma;
						}else{
							constructorItems = constructorItems+parArray.get(j).getType()+" "+parArray.get(j).getName()+lastComma;
						}
					}
				}		
				data[i] = "\tpublic "+m.getType()+" "+m.getName()+"("+constructorItems+") {";
				for(int l=0; l<extraCodeLength(m);l++){
					data[l+i+1] = addTabs(arrayListToArray(m.getExtraCode()))[l];
				}
				data[i+1+extraCodeLength(m)] = "\t}";
				i=i+2+extraCodeLength(m);
			}
		}else{
			data = new String[1];
			data[0] = "";
		}
		return data;
	}
	private String[] arrayListToArray(ArrayList<String> a){
		String[] out = new String[a.size()];
		for(int i = 0; i<a.size();i++){
			out[i] = a.get(i);
		}
		return out;
	}
}
